from flask import Flask, g, request,session,current_app

app = Flask(__name__)

def test_func():
    print(request)
    print(g)
app.secret_key='aaa'

app.config['REDIS'] = 'redis://127.0.0.1/1'

@app.before_request
def before_request():
    test_func()
    print('before_request')
    g.user_id = 123456

from Day02.user import user_bp
app.register_blueprint(user_bp,url_prefix='/user')

if __name__ == '__main__':
    # 在请求上下文外面，使用请求上下文对象。报错
    # print(request.url)
    # print(session.get('name'))

    # 构造一个测试的请求上下文， 4个上下文对象都可用
    # with app.test_request_context('/'):
    #     print(request.url)
    #     print(session.get('name'))
    #     print(current_app)
    #     print(g)

    # 构造一个应用上下文，在应用上下文中，current_app, g. request和session不可用
    # with app.app_context():
    # print(request.url)
    # print(session.get('name'))
    # print(current_app)
    # print(g)

    # 在请求上下文外面
    # test_func()
    # app.run(debug=True)

    environ = {'wsgi.version': (1, 0), 'wsgi.input': '', 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/',
               'SERVER_NAME': 'itcast server', 'wsgi.url_scheme': 'http', 'SERVER_PORT': '80'}
    with app.request_context(environ):
        print(request.url)
        print(session.get('name'))
        print(current_app)
        print(g)

